import {observable, computed, action} from 'mobx'
import {createContext} from 'react'
import ChildrenPageUtils from '@/store/extends'
import {uSelectDevStatusAndSimpleData} from '@/api/setting'

class Baseinfoedit extends ChildrenPageUtils {
  @observable pageQuery: {devId: string} = {devId: ''}
  @observable pageData: Setting.DevBaseInfo | null = null
  @observable devName: string = ''
  @observable address: string = ''
  @observable provinces: string[] = []

  @action.bound
  changeProvinces(val: string[]) {
    this.provinces = val
  }

  @action.bound
  async updatePageDataSource() {
    let res = await uSelectDevStatusAndSimpleData({
      devId: this.pageQuery.devId
    }).catch(err => {})
    if (res && res.result === 'success') {
      this.pageData = res.data
      this.devName =
        this.pageData?.devEditDataPackList.filter(
          item => item.devBaseDataName === '设备名称'
        )[0]?.devBaseDataValue || ''
      this.address =
        this.pageData?.devEditDataPackList.filter(
          item => item.devBaseDataName === '设备地址'
        )[0]?.devBaseDataValue || ''
      this.provinces = this.pageData?.devEditDataPackList
        .filter(item => ['省', '市', '区'].includes(item.devBaseDataName))
        .map(item =>
          item.devBaseDataValue !== 'N/A' && item.devBaseDataValue
            ? item.devBaseDataValue
            : ''
        )
    } else {
      this.pageData = null
      this.devName = ''
      this.address = ''
      this.provinces = []
    }
  }
}
export const beSharedBaseInfoEditStore = createContext(new Baseinfoedit())
